<?php

namespace app\admin\controller;

use app\admin\library\Auth;
use app\common\controller\Backend;
use app\admin\model\Setting as SettingModel;
use fast\Random;
use fast\Tree;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Validate;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Setting extends Backend
{

    protected $model = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Setting();
    }

    public function test()
    {
        $curDate = '2024-04-25';
        $start = '';
        $end = '';
        $rentPills = [
            ['start' => '04:00', 'end' => '04:29' ],
            ['start' => '04:30', 'end' => '04:59' ],
            ['start' => '05:00', 'end' => '05:29' ],

            ['start' => '06:30', 'end' => '06:59' ],
            ['start' => '07:00', 'end' => '07:29' ],
            ['start' => '07:30', 'end' => '07:59' ],
            ['start' => '08:00', 'end' => '08:29' ],
            ['start' => '08:30', 'end' => '08:59' ],
          
            ['start' => '13:00', 'end' => '13:29' ],
            ['start' => '13:30', 'end' => '13:59' ],
        ];
        $things = [];
        foreach ($rentPills as $pill) {
          // 得到这个pill的完整的开始和结束时间，包括的年月日
          $pillStart = $curDate . ' ' . $pill['start'];
          $pillEnd = $curDate . ' ' . $pill['end'];
          // echo 'pillStart => ' . $pillStart . "---" . $pillEnd . '<br />';
          if (empty($start)) {
              $start = $pillStart;
          }
          if (empty($end)) {
              $end = $pillEnd;
          }
          // echo 'start =>' . $start . "---" . $end . '<br />';
          // 非一样时间时，计算相差时间是否为30分钟
          if ($start != $pillStart) {
            $tmp1X = strtotime($end);
            $tmp2X = strtotime($pillEnd);
            
            // echo $tmp1X . "---" . $tmp2X . '<br />';
            $diffX = $tmp2X - $tmp1X;
            if ($diffX != 1800) {
              // echo 'diff <> 1800 =>' . $end . "---" . $pillEnd . '<br />';
              $things[] = $start . '-' . $end;
              $start = $pillStart;
              $end = $pillEnd;
            } else {
              // echo 'diff = 1800 =>' . $end . "---" . $pillEnd . '<br />';
              $end = $pillEnd;
            }
          }
          // echo "******************************<br />";
        }
        $things[] = $start . '-' . $end;
        return $this->view->fetch();
    }

    public function rentEdit($ids = null)
    {
        $allSettings = SettingModel::getSettings(); 
        /*
        $rentReserveAdvanceDay = $this->model->where('name', 'rent_reserve_advance_day')->find();
        $rentAtLeastMin = $this->model->where('name', 'rent_at_least_min')->find();
        $rentRefund = $this->model->where('name', 'rent_refund')->find();
        $unlockInadvanceMin = $this->model->where('name', 'unlock_inadvance_min')->find();
        // 增加的信用分以及赠送比例
        $rentCancel = $this->model->where('name', 'rent_cancel')->find();
        $addCredit = $this->model->where('name', 'add_credit')->find();
        $declineCredit = $this->model->where('name', 'decline_credit')->find();
        $rechargeBonus = $this->model->where('name', 'recharge_bonus')->find();
        $refundMoney = $this->model->where('name', 'refund_money')->find();
        */
        $rentReserveAdvanceDay = SettingModel::getSingleSetting('rent_reserve_advance_day', $allSettings);
        $rentAtLeastMin = SettingModel::getSingleSetting('rent_at_least_min', $allSettings);
        $rentRefund = SettingModel::getSingleSetting('rent_refund', $allSettings);
        $unlockInadvanceMin = SettingModel::getSingleSetting('unlock_inadvance_min', $allSettings);;
        // 增加的信用分以及赠送比例
        $rentCancel = SettingModel::getSingleSetting('rent_cancel', $allSettings);;
        $addCredit = SettingModel::getSingleSetting('add_credit', $allSettings);;
        $declineCredit = SettingModel::getSingleSetting('decline_credit', $allSettings);;
        $rechargeBonus = SettingModel::getSingleSetting('recharge_bonus', $allSettings);;
        $refundMoney = SettingModel::getSingleSetting('refund_money', $allSettings);;
        $cancelRule = SettingModel::getSingleSetting('cancel_rule', $allSettings);;
        if ($this->request->isPost()) {
            SettingModel::where('name', 'rent_reserve_advance_day')
              ->update(['value' => $this->request->post('rent_reserve_advance_day')]);

            SettingModel::where('name', 'rent_at_least_min')
              ->update(['value' => $this->request->post('rent_at_least_min')]);

            SettingModel::where('name', 'unlock_inadvance_min')
              ->update(['value' => $this->request->post('unlock_inadvance_min')]);

            SettingModel::where('name', 'rent_refund')
              ->update(['value' => $this->request->post('rent_refund')]);

            SettingModel::where('name', 'rent_cancel')
              ->update(['value' => $this->request->post('rent_cancel')]);

            SettingModel::where('name', 'add_credit')
              ->update(['value' => $this->request->post('add_credit')]);

            SettingModel::where('name', 'decline_credit')
              ->update(['value' => $this->request->post('decline_credit')]);

            SettingModel::where('name', 'recharge_bonus')
              ->update(['value' => $this->request->post('recharge_bonus')]);

            SettingModel::where('name', 'refund_money')
              ->update(['value' => $this->request->post('refund_money')]);

            SettingModel::where('name', 'cancel_rule')
              ->update(['value' => $this->request->post('cancel_rule')]);

            $this->success("保存成功");

        }
        $this->view->assign('rent_reserve_advance_day', $rentReserveAdvanceDay);
        $this->view->assign('rent_at_least_min', $rentAtLeastMin);
        $this->view->assign('rent_refund', $rentRefund);
        $this->view->assign('unlock_inadvance_min', $unlockInadvanceMin);

        $this->view->assign('rent_cancel', $rentCancel);
        $this->view->assign('add_credit', $addCredit);
        $this->view->assign('decline_credit', $declineCredit);
        $this->view->assign('recharge_bonus', $rechargeBonus);
        $this->view->assign('refund_money', $refundMoney);
        $this->view->assign('cancel_rule', $cancelRule);
        return $this->view->fetch();
    }


}
